package com.axzhengxin.platform.business.service.impl;

import com.axzhengxin.platform.business.domain.*;
import com.axzhengxin.platform.business.mapper.*;
import com.axzhengxin.platform.business.service.IBizGuaranteeService;
import com.axzhengxin.platform.common.util.IdUtils;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.mapper.SysDeptMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 担保业务IMPL
 *
 * @author ahzj0005
 */
@Slf4j
@Service
public class BizGuaranteeServiceImpl implements IBizGuaranteeService {

    @Autowired
    private BizApplyWarrantRelativeMapper relativeMapper;
    @Autowired
    private BizWarrantInfoMapper warrantInfoMapper;
    @Autowired
    private BizApplyInfoMapper bizApplyInfoMapper;

    @Autowired
    private BizFlowInfoMapper bizFlowInfoMapper;

    @Autowired
    private BizFlowTaskMapper bizFlowTaskMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private IdUtils idUtils;

    /**
     * 新增担保 并启动流程
     *
     * @param bizApplyWarrantRelative
     * @return
     */
    @Override
    public AjaxResult applyGuarantee(BizApplyWarrantRelative bizApplyWarrantRelative) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String applyId = bizApplyWarrantRelative.getApplyId();
        BizApplyInfo bizApplyInfo = bizApplyInfoMapper.selectBizApplyInfoById(applyId);
        String productId = bizApplyInfo.getProductId();
        String productName = bizApplyInfo.getProductName();
        BigDecimal approveMount = bizApplyWarrantRelative.getApproveMount();
        BigDecimal financeAmount = bizApplyInfo.getFinanceAmount();
        if (financeAmount.compareTo(approveMount) < 0) {
            return AjaxResult.error("预授信额度设置过大，当前最高可设置为" + financeAmount.setScale(2, RoundingMode.HALF_UP) + "万元！");
        }
        //新增流程
        BizFlowInfo bizFlowInfo = buildFlowInfo("GUARANTEE", 1010, bizApplyWarrantRelative.getApplyId(), user);
        bizFlowInfoMapper.insertBizFlowInfo(bizFlowInfo);
        BizFlowTask bizFlowTask = buildFlowTask("GUARANTEE", 1010, bizApplyWarrantRelative.getApplyId(), user);
        bizFlowTask.setFlowUid(bizFlowInfo.getFlowUid());
        bizFlowTaskMapper.insertBizFlowTask(bizFlowTask);


        BizWarrantInfo bizWarrantInfo = new BizWarrantInfo();

        bizWarrantInfo.setWarrantId(idUtils.getSnakeId());
        bizWarrantInfo.setBizApplyInfo(bizApplyInfo);
        bizWarrantInfo.setFlowUid(bizFlowInfo.getFlowUid());
        bizWarrantInfo.setPublishDate(bizApplyInfo.getPubTime());
        bizWarrantInfo.setApproveAmount(approveMount);
        bizWarrantInfo.setCreateBy(user.getUserId().toString());
        bizWarrantInfo.setFinanceOrg(user.getDept().getDeptName());
        bizWarrantInfo.setProductId(productId);
        bizWarrantInfo.setProductName(productName);
        warrantInfoMapper.insertBizWarrantInfo(bizWarrantInfo);
        bizApplyWarrantRelative.setWarrantContractNo(UUID.randomUUID().toString().substring(0, 6));
        bizApplyWarrantRelative.setWarrantId(bizWarrantInfo.getWarrantId());
        bizApplyWarrantRelative.setFlowUid(bizFlowInfo.getFlowUid());
        bizApplyWarrantRelative.setCreateBy(user.getUserId().toString());

        relativeMapper.insertBizApplyWarrantRelative(bizApplyWarrantRelative);
        return AjaxResult.success();
    }

    @Override
    public List<SysDept> getGuaranteeCom() {
        List<SysDept> deptTypeList = sysDeptMapper.getDeptTypeList(3);

        return deptTypeList;
    }

    @Override
    public BizWarrantInfo detail(String warrantId) {
        BizWarrantInfo bizWarrantInfo = warrantInfoMapper.selectBizWarrantInfoById(warrantId);

        return bizWarrantInfo;
    }


    private BizFlowInfo buildFlowInfo(String bizType, Integer status, String bizId, SysUser user) {
        Long userId = user.getUserId();
        BizFlowInfo bizFlowInfo = new BizFlowInfo();
        bizFlowInfo.setBizType(bizType);
        /*bizFlowInfo.setCreateTime(new Date());*/
        bizFlowInfo.setBizId(bizId);
        bizFlowInfo.setFinanceId(user.getDept().getFinanceId());
        bizFlowInfo.setCurrentNodeUserId(String.valueOf(userId));
        bizFlowInfo.setCurrentNodeDeptId(String.valueOf(user.getDeptId()));
        bizFlowInfo.setStatus(status);
        bizFlowInfo.setCreateBy(String.valueOf(userId));
        return bizFlowInfo;
    }

    public BizFlowTask buildFlowTask(String bizType, Integer status, String bizId, SysUser user) {
        Long userId = user.getUserId();

        BizFlowTask bizFlowTask = new BizFlowTask();
        bizFlowTask.setStatus(status);
        /*bizFlowTask.setCreateTime(new Date());*/
        bizFlowTask.setCreateBy(String.valueOf(userId));
        bizFlowTask.setBizId(bizId);
        bizFlowTask.setBizType(bizType);
        bizFlowTask.setUserId(String.valueOf(userId));
        return bizFlowTask;
    }
}
